#!/bin/bash

function trans()
{
    echo "$1" | awk '{for (i=1;i<=NF;i++) {\
        if ($i=="41") printf("A");\
        if ($i=="42") printf("B");\
        if ($i=="43") printf("C");\
        if ($i=="44") printf("D");\
        if ($i=="45") printf("E");\
        if ($i=="46") printf("F");\
        if ($i=="47") printf("G");\
        if ($i=="48") printf("H");\
        if ($i=="49") printf("I");\
        if ($i=="4a") printf("J");\
        if ($i=="4b") printf("K");\
        if ($i=="4c") printf("L");\
        if ($i=="4d") printf("M");\
        if ($i=="4e") printf("N");\
        if ($i=="4f") printf("O");\
        if ($i=="50") printf("P");\
        if ($i=="51") printf("Q");\
        if ($i=="52") printf("R");\
        if ($i=="53") printf("S");\
        if ($i=="54") printf("T");\
        if ($i=="55") printf("U");\
        if ($i=="56") printf("V");\
        if ($i=="57") printf("W");\
        if ($i=="58") printf("X");\
        if ($i=="59") printf("Y");\
        if ($i=="5a") printf("Z");\
    }\
    printf("\n");\
    }'
}

function byte()
{
    # byte 0 of "*"
    INDEX=$1
    OF=$2 # always be "of"
    SOURCE=$3
    echo $SOURCE | awk '{print $"'"$INDEX"'"}'  
}

function first()
{
    # first byte "00" of "*"
    BYTE=$1
    VALUE=$2
    OF=$3
    SOURCE=$4

    echo $SOURCE | awk '{for (i=1;i<=NF;i++) if ($i == "'"$VALUE"'") print i}'
}

function bytes()
{
    # bytes from 5 of "*"
    FROM=$1 # always be "FROM"
    FROM_INDEX=$2
    TO=$3
    TO_INDEX=$4
    OF=$5    # always be "of"
    SOURCE=$6
    [ "$TO_INDEX" == "end" ] && TO_INDEX=0;
    echo $SOURCE | awk 'BEGIN{ start = "'"$FROM_INDEX"'"; end = "'"$TO_INDEX"'"; start+=0; end+=0 }{ if ( end==0 ) end=NF; for (i=start;i<=end;i++) printf("%s ", $i); printf("\n")}'
}

function no_x()
{
    echo "$1" | awk '{ gsub("0x", "", $0); print $0 }'
}

function ipmi_cmd()
{
    CMD="$1"
    [ -f /usr/bin/ipmitool ] && /usr/bin/ipmitool raw $CMD | awk '{for (i=1;i<=NF;i++) printf("%s ", $i); printf("\n")}' && return
    CMD=`no_x "$1"`
    [ -f /usr/bin/ipmicmd ] && /usr/bin/ipmicmd -k "0f 00 $CMD" smi 0 | awk '{for (i=6;i<=NF;i++) printf("%s ", $i); printf("\n")}'
}

function print_result()
{
    result="$1"
    data=`bytes from 5 to end of "$result"`
    status=`byte 2 of "$result"`
    [ "$status" == "88" ] && exit 0

    let sep=`first byte "00" of "$data"`
    name=`bytes from 1 to $sep of "$data"`
    let sep=sep+1
    value=`bytes from $sep to end of "$data"`
    echo `trans "$name"` "	=	" $value
}

#byte 4 of "02 00 04 88 00 00"
#bytes from 5 to end of "06 00 08 00 43 51 48 52 42 4c 00 01"
#first byte "00" of "43 51 48 52 42 4c 00 01"
#print_result "06 00 08 00 43 51 48 52 42 4c 00 01"

echo "" | awk '{for(i=0;i<100;i++) printf("%02X\n", i) }'| while read index
do
    CMD="0x36 0x02 0x06 0x0a 0x00 0x$index 0x00"
    result=`ipmi_cmd "$CMD"`
    if [ `byte 2 of "$result"` != "88" ]
    then
        echo "SEND:> "`no_x "$CMD"`
        echo "RECV:< $result"
        echo "MEAN:  "`print_result "$result"`
        echo ""
    fi
done

exit 0

